<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="icon" href="./../images/favico.ico">
    <!--element-ui的样式-->
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css"/>
    <!-- 引入样式  -->
    <link rel="stylesheet" href="../styles/index.css"/>
    <!--引入vant样式-->
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <!--本页面内容的样式-->
    <link rel="stylesheet" href="./../styles/user.css"/>
    <link rel="stylesheet" href="../styles/common.css"/>
    <link rel="stylesheet" href="../styles/page.css"/>
    <style>
        .selectInput {
            position: relative;
            width: 100%;
            min-width: 100px;
        }

        .selectInput .flavorSelect {
            position: absolute;
            width: 100%;
            padding: 0 10px;
            border-radius: 3px;
            border: solid 1px #FF903D;
            line-height: 30px;
            text-align: center;
            background: #fff;
            top: 50px;
            z-index: 99;
        }

        .selectInput .flavorSelect .items {
            cursor: pointer;
            display: inline-block;
            width: 100%;
            line-height: 35px;
            border-bottom: solid 1px #f4f4f4;
            color: #666;
        }

        .selectInput .flavorSelect .none {
            font-size: 14px;
        }

        #food-add-app .uploadImg .el-form-item__label::before {
            content: '*';
            color: #F56C6C;
            margin-right: 4px;
        }

    </style>
</head>
<body>
<div class="addBrand-container" id="food-add-app">
    <div class="container">
        <el-form
                ref="ruleForm"
                :model="ruleForm"
                :rules="rules"
                :inline="true"
                label-width="80px"
                class="demo-ruleForm"
        >
            <div>
                <el-form-item
                        label="用户名:"
                        prop="name"
                >
                    <el-input
                            v-model="ruleForm.name"
                            placeholder="请填写用户名"
                            maxlength="20"
                    />
                </el-form-item>
            </div>
            <div>
                <el-form-item
                        label="手机号:"
                        prop="phone"
                >
                    <el-input
                            v-model="ruleForm.phone"
                            placeholder="请设置手机号"
                    />
                </el-form-item>
            </div>
            <div>
                <el-form-item
                        label="性别:"
                        prop="sex"
                >
                    <el-radio-group v-model="ruleForm.sex">
                        <el-radio label="男"></el-radio>
                        <el-radio label="女"></el-radio>
                    </el-radio-group>
                </el-form-item>
            </div>
            <div>
                <el-form-item
                        label="身份证号:"
                        prop="idNumber"
                >
                    <el-input
                            v-model="ruleForm.idNumber"
                            placeholder="请输入身份证号"
                            maxlength="20"
                    />
                </el-form-item>
            </div>

            <div>
                <el-form-item
                        label="头像:"
                        prop="avatar"
                        class="uploadImg"
                >
                    <el-upload
                            class="avatar-uploader"
                            action="/common/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :on-change="onChange"
                            ref="upload"
                            border="0px"
                    >
                        <el-avatar
                                v-if="imageUrl"
                                :src="imageUrl"
                                class="avatar"
                                :size="150"
                        ></el-avatar>
                        <i
                                v-else
                                class="el-icon-plus avatar-uploader-icon"
                        ></i>
                    </el-upload>
                </el-form-item>
            </div>
            <!--  <div>
                  <el-form-item
                          label="头像:"
                          prop="avatar"
                          class="uploadImg"
                  >
                      <el-upload
                              class="avatar-uploader"
                              action="/common/upload"
                              :show-file-list="false"
                              :on-success="handleAvatarSuccess"
                              :on-change="onChange"
                              ref="upload"
                              border="0px"
                      >
                          <el-avatar
                                  v-if="imageUrl"
                                  :src="imageUrl"
                                  class="avatar"
                                  :size="150"
                          ></el-avatar>
                          <i
                                  v-else
                                  class="el-icon-plus avatar-uploader-icon"
                          ></i>
                      </el-upload>
                  </el-form-item>
              </div>-->

            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()">
                        取消
                    </el-button>
                    <el-button
                            type="primary"
                            @click="submitForm('ruleForm')"
                    >
                        保存
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant组件 -->
<script src="./../js/vant.min.js"></script>
<!--不同屏幕尺寸根字体设置-->
<script src="./../js/base.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script src="./../api/order.js"></script>
<script src="./../api/login.js"></script>
<script src="./../api/main.js"></script>
<script src="./../api/user.js"></script>
<script src="../../backend/js/validate.js"></script>
<script>
    new Vue({
        el: '#food-add-app',
        data() {
            return {
                id: '',
                value: '',
                imageUrl: '',
                ruleForm: {
                    id: '',
                    name: '',
                    phone: '',
                    sex: '男',
                    idNumber: '',
                    avatar: '',
                    status: 1
                },
                mak: false
            }
        },
        computed: {
            rules() {
                return {
                    'name': [
                        {'required': true, 'message': '请填写用户名', 'trigger': 'blur'}
                    ],
                    'phone': [{'required': true, 'validator': checkPhone, 'trigger': 'blur'}],
                    'idNumber': [{'required': true, 'validator': validID, 'trigger': 'blur'}]
                }
            }
        },
        created() {
            this.init()
        },
        mounted() {
        },
        methods: {
            async init() {
                /*  const params = {
                      ...this.ruleForm,
                      sex: this.ruleForm.sex === '女' ? '0' : '1'
                  }*/
                await getUserApi().then(res => {
                    console.log(res)
                    if (String(res.code) === '1') {
                        this.ruleForm = {...res.data}
                        this.ruleForm.sex = res.data.sex === '0' ? '女' : '男'
                        this.imageUrl = `/common/download?name=${res.data.avatar}`;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },

            submitForm(formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let params = {...this.ruleForm}
                        params.sex = this.ruleForm.sex === '女' ? '0' : '1'
                        params.status = this.ruleForm ? 1 : 0
                        if (!this.imageUrl) {
                            this.$message.error('请上传头像')
                            return
                        }
                        editUser(params).then(res => {
                            if (res.code === 1) {
                                sessionStorage.setItem("userPhone", JSON.stringify(params))
                                this.$message.success('个人信息修改成功！')
                                this.goBack()
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })

                    } else {
                        return false
                    }
                })
            },


            handleAvatarSuccess(response, file, fileList) {
                // 拼接down接口预览
                if (response.code === 0 && response.msg === '未登录') {
                    window.top.location.href = '/backend/page/login/login.html'
                } else {
                    this.imageUrl = `/common/download?name=${response.data}`
                    this.ruleForm.avatar = response.data
                }
            },

            onChange(file) {
                if (file) {
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if (['png', 'jpeg', 'jpg'].indexOf(suffix) < 0) {
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if (!size) {
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            },

            goBack() {
                location.href = '/front/page/user.html';
            }
        }
    })
</script>
</body>
</html>
